<?php
namespace app\admin\inter_logic;
use app\admin\inter_model\CarInsureTranModel;
use app\admin\inter_model\CarTranModel;
use app\admin\inter_model\DriverAppTranModel;
use app\admin\inter_model\DriverCourseTranModel;
use app\admin\inter_model\DriverTranModel;
use app\inter\model\DriverMesModel;
use app\inter\model\DriverModel;
use app\inter\model\DriverMoneyLogModel;
use app\inter\model\DriverMoneyModel;
use think\Db;
use think\Model;

class DriverLogic extends Model
{

    /**
     * 根据条件获取司机信息
     * @param $map
     * @param $field
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getDriver($map,$field='*',$page=1,$limit=10 )
    {
        $model = new DriverModel();
        return $model->getDriver($map,$field,true,$page,$limit);
    }

    /**
     * 更新司机信息
     * @param $map
     * @param $data
     * @return array|false
     * */
    public function renewDriver($map,$data)
    {
        if(empty($data)){
            return false;
        }
        $model = new DriverModel();
        return $model->updateDriver($map,$data);
    }

    /**
     * 根据司机id获取司机信息
     * @param $did
     * @param $field
     * @return array|false
     * */
    public function getDriverByDid($did,$field='a.*')
    {
        return Db::table("xm_driver_info a")
            ->join(['xm_driver_base'=> 'e'],'e.driver_id = a.driver_id', 'LEFT')
            ->join('__REGION__ b','b.region_id=a.work_province','LEFT')
            ->join('__REGION__ c','c.region_id=a.work_city','LEFT')
            ->join('__REGION__ d','d.region_id=a.work_county','LEFT')
            ->field($field.",b.region_name as province,c.region_name as city,d.region_name as county")
            ->where(array('a.driver_id'=>$did))->find();
    }

    /**
     * 审核时添加交通部需要的数据
     * @param $data
     * */
    public function createTranInfo($data)
    {
        $driver_base_model = new DriverTranModel();
        $driver_app_model = new DriverAppTranModel();
        $driver_course_model = new DriverCourseTranModel();
        $car_base_model = new CarTranModel();
        $car_insure_model = new CarInsureTranModel();
        $data_driver_base = $data['driver']['driver_base']?:array();
        $data_driver_app = $data['driver']['app']?:array();
        $data_driver_course = $data['driver']['course']?:array();
        $data_car_base = $data['car']['car_base']?:array();
        $data_car_insurance = $data['car']['insurance']?:array();
        $data_driver_base['full_time_driver']&&$full_time_driver=$data_driver_base['full_time_driver'];
        unset($data_driver_base['full_time_driver']);
        /****************交通部司机基本信息数据处理*****************************/
        foreach ($data_driver_base as $key => $val){
            if($this->judgeTime($key)){
                $data_driver_base[$key] = strtotime($val);
            }
            if(empty($val))unset($data_driver_base[$key]);
        }
        $full_time_driver&&$data_driver_base['full_time_driver'] = $full_time_driver;
        $data_driver_base&&$data_driver_base['driver_id'] = $data['driver_id'];
        /****************交通部司机移动终端信息数据处理*****************************/
        foreach ($data_driver_app as $key => $val){
            if($this->judgeTime($key)){
                $data_driver_app[$key] = strtotime($val);
            }
            if(empty($val))unset($data_driver_app[$key]);
        }
        $data_driver_app&&$data_driver_app['driver_id'] = $data['driver_id'];
        /****************交通部司机培训信息数据处理*****************************/
        foreach ($data_driver_course as $key => $val){
            if($this->judgeTime($key)){
                $data_driver_course[$key] = strtotime($val);
            }
            if(empty($val))unset($data_driver_course[$key]);
        }
        $data_driver_course&&$data_driver_course['driver_id'] = $data['driver_id'];
        /****************交通部车辆信息数据处理*****************************/
        foreach ($data_car_base as $key => $val){
            if($this->judgeTime($key)){
                $data_car_base[$key] = strtotime($val);
            }
            if(empty($val))unset($data_car_base[$key]);
        }
        $data_car_base&&$data_car_base['car_id'] = $data['car_id'];
        /****************交通部车辆保险信息数据处理*****************************/
        foreach ($data_car_insurance as $key => $val){
            if($this->judgeTime($key)){
                $data_car_insurance[$key] = strtotime($val);
            }
            if(empty($val))unset($data_car_insurance[$key]);
        }
        $data_car_insurance&&$data_car_insurance['car_id'] = $data['car_id'];
        $data_driver_base&&$driver_base_model->addDriverTran($data_driver_base);
        $data_driver_app&&$driver_app_model->addDriverAppTran($data_driver_app);
        $data_driver_course&&$driver_course_model->addDriverCourseTran($data_driver_course);
        $data_car_base&&$car_base_model->addCarTran($data_car_base);
        $data_car_insurance&&$car_insure_model->addCarInsureTran($data_car_insurance);
    }

    /**
     * 用于判断包含时间英文的字符串
     * @param string //需要判断的字符串
     * @param array //补充的判断的字符串
     * @return boolean
     * */
    protected function judgeTime($key,$date= array('insureff','insurexp','driver_birthday'))
    {
        if(empty($key)){
            return false;
        }
        if(strpos($key,'date')!==false || strpos($key,'_on')!==false ||
            strpos($key,'_off')!==false || in_array($key,$date) || strpos($key,'time')!==false){
            return true;
        }
        return false;
    }

    /**
     * 添加司机余额
     * @param $data
     * @return array|false
     * */
    public function createDriverMoney($data)
    {
        $model = new DriverMoneyModel();
        return $model->addMoney($data);
    }

    /**
     * 根据司机id，获取司机账户信息
     * @param $did
     * @param $field
     * @return array|false
     * */
    public function getDriverMoneyByDid($did,$field='a.*')
    {
        $model = new DriverMoneyModel();
        $map['driver_id'] = $did;
        return $model->getDriverMoney($map,$field,false);
    }

    /**
     * 添加司机信息
     * @param $data
     * */
    public function createDriverMsg($data,$did)
    {
        $model = new DriverMesModel();
        $data['send_time'] = time();
        $data_msg_relation['mes_id'] = $model->addDriverMes($data);
        if($data_msg_relation['mes_id']){
            $data_msg_relation['driver_id'] = $did;
            $model->addDriverMesRelation($data_msg_relation);
        }
    }

    /**
     * 获取司机订单流水
     * @param $map
     * @param $field
     * @param $flag //一条或多条
     * @param $page
     * @param $limit
     * @return  mixed
     * */
    public function getDriverOrderFlow($map,$field='a.*',$flag = true,$page=1,$limit=10)
    {
        $model = new DriverMoneyLogModel();
        return $model->getLog($map,$field,$flag ,$page,$limit);
    }

}
